<!DOCTYPE html>
<html lang="zh_CN">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<title>更新提示</title>
	<link rel="stylesheet" href="css/css.css">
	<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/v-tap.js" type="text/javascript" charset="utf-8"></script>
	<script src="html5plus://ready"></script>
</head>
<body>
	<div class="app" id="app">
		<div class="content">
			<h3 class="confirm-title">更新提示</h3>
			<div class="confirm-content" v-html="content"></div>
			<div class="confirm-foot">
				<span class="confirm-btn cancel" v-tap="[cancel]" v-if="forcedUpdate">取消</span>
				<span class="confirm-btn confirm" v-tap="[confirm]">确认更新</span>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		let w = plus.webview.currentWebview()
		console.log(w)
		plus.key.addEventListener('backbutton', function(){
			return false
		}, false);
		
		var vm = new Vue({
			el: '#app',
			data: function(){
				return {
					content: w.content,
					forcedUpdate: w.forcedUpdate
				}
			},
			methods: {
				cancel: function(){
					w.close()
				},
				downloadPackage: function(){
					var url = w.hotPackageUrl
					var _this = this
					var waiting = plus.nativeUI.showWaiting('下载更新中');
					var totalSize = 0
					var dtask = plus.downloader.createDownload(url, { method: 'GET', filename: '_downloads/'}, function(d,status){
						plus.nativeUI.closeWaiting();
						if(status == 200){
							_this.installPack(d.filename);
						}else{//下载失败
							plus.nativeUI.closeWaiting();
							plus.nativeUI.toast('下载失败');
							w.close()
						}
					});
					dtask.addEventListener('statechanged', function(download, status){
						if(download.state == 2){
							totalSize = download.totalSize
							totalSize = Math.round(parseInt(totalSize) / (1024 * 1024 / 100)) / 100;
						}
						if(download.state == 3 && totalSize){
							//已下载 download.downloadedSize
							//任务大小 download.totalSize
							var ds = Math.round(parseInt(download.downloadedSize) / (1024 * 1024 / 100)) / 100;
							waiting.setTitle('更新进度:'+ds+'M/'+totalSize+'M');
						}
					}, false);
					dtask.start();
				},
				confirm: function(){
					if(w.downUrl && w.downUrl.android){
						plus.runtime.openWeb(w.downUrl.android)
					}else if(w.downUrl && w.downUrl.ios){
						plus.runtime.openWeb(w.downUrl.ios)
					}else{
						this.downloadPackage()
					}
				},
				//安装
				installPack: function(path){
					var _this = this
					plus.nativeUI.showWaiting("安装更新文件中...");
					plus.runtime.install(path,{},function(){
						plus.nativeUI.closeWaiting();
						_this.tipReload()
					},function(e){
						plus.nativeUI.closeWaiting();
						plus.nativeUI.toast('安装失败')
					});
				},
				//提示重启
				tipReload: function(){
					if(plus.os.name == 'Android'){
						plus.nativeUI.confirm('最新版安装完成，是否立即重启APP?', function(e){
							if(e.index == 0){
								plus.runtime.restart();
					        }
						})
					}else{
						plus.nativeUI.alert('更新成功,重启APP后即可使用最新版！', function(){
							w.close()
						});	
					}
				}
			}
		})
	</script>
</body>
</html>